package com.sihai.ehr;

import java.net.URL;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mortbay.thread.QueuedThreadPool;

public class EHRServerRunner {

	private static Server server = null;

	public static void run() throws Exception {
		if (server != null) {
			return;
		}

		server = new Server();
		QueuedThreadPool boundedThreadPool = new QueuedThreadPool();
		boundedThreadPool.setMaxThreads(1024);
		server.setThreadPool(boundedThreadPool);

		Connector connector = new SelectChannelConnector();
		// connector.setHost("localhost");
		connector.setPort(8099);
		server.addConnector(connector);

		server.setStopAtShutdown(true);
		server.setSendServerVersion(true);

		URL url = EHRServerRunner.class.getResource("/");
		String webapp_path = url.getFile().replaceAll("target/(.*)",
				"src/main/webapp");
		System.out.println(webapp_path);

		WebAppContext context = new WebAppContext(webapp_path, "/");
		server.setHandler(context);

		// context.addServlet("com.sdo.infra.bpm.console.web.ContinuationServlet",
		// "/Continuation");

		server.start();
	}

	public static void stop() throws Exception {
		if (server != null) {
			server.stop();
			server = null;
		}
	}

	
	public static void main(String[] args) throws Exception {
		run();
		server.join();

	}
}
